package com.tss.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tss.mapper.CourseMapper;
import com.tss.pojo.Course;
import com.tss.pojo.CourseExample;
import com.tss.pojo.CourseExample.Criteria;
import com.tss.pojo.Result;
import com.tss.service.CourseService;
import com.tss.utils.BeanUtils;
import com.tss.vo.Dictionary;

@Service
public class CourseServiceImpl implements CourseService {
	
	@Autowired
	private CourseMapper courseMapper;
	
	

	@Override
	public Result createCourse(Course course) {
		Result result = new Result();
		if(course==null||course.getName()==null){
			result.setErrorMessage("参数错误！");
			return result;
		}
		CourseExample example = new CourseExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(course.getName());
		criteria.andValidateEqualTo(true);
		List<Course> list = courseMapper.selectByExample(example);
		if(!list.isEmpty()){
			result.setErrorMessage("课程名称已存在！");
			return result;
		}
		
		course.setValidate(true);
		course.setStatus(Dictionary.COURSE_STATUS_BEFORE);
		courseMapper.insert(course);
		result.setSuccessMessage("新建课程成功！");
		return result;
	}



	@Override
	public Result updateCourse(Course course) {
		Result result = new Result();
		if(course==null||course.getId()==null){
			result.setErrorMessage("参数为空！");
			return result;
		}
		Course c = courseMapper.selectByPrimaryKey(course.getId());
		if(c==null){
			result.setErrorMessage("课程不存在！");
			return result;
		}
		c=BeanUtils.updateBean(c, course);
		courseMapper.updateByPrimaryKey(c);
		result.setSuccessMessage("修改成功！");
		return result;
	}



	@Override
	public Result deleteCourse(Long id) {
		Result result = new Result();
		Course course = courseMapper.selectByPrimaryKey(id);
		if(course==null){
			result.setErrorMessage("删除课程失败！");
			return result;
		}
		course.setValidate(false);
		courseMapper.updateByPrimaryKey(course);
		result.setSuccessMessage("删除课程成功！");
		return result;
		
	}



	@Override
	public List<Course> findAllCourse() {
		CourseExample example = new CourseExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		List<Course> list = courseMapper.selectByExample(example);
		return list;
	}



	@Override
	public Integer countAllCourses() {
		CourseExample example = new CourseExample();
		Criteria criteria = example.createCriteria();
		criteria.andValidateEqualTo(true);
		Integer count = courseMapper.countByExample(example);
		return count;
	}



	@Override
	public Course findCourse(Long id) {
		Course course = courseMapper.selectByPrimaryKey(id);
		return course;
	}



	
	
	@Override
	public Result overCourse(Long id) {
		Result result = new Result();
		Course course = courseMapper.selectByPrimaryKey(id);
		if(course==null){
			result.setErrorMessage("课程不存在！");
			return result;
		}
		if(course.getStatus()==Dictionary.COURSE_STATUS_OVER){
			result.setErrorMessage("请不要重复结课！");
			return result;
		}
		course.setStatus(Dictionary.COURSE_STATUS_OVER);
		courseMapper.updateByPrimaryKey(course);
		result.setSuccessMessage("结课成功！");
		return result;
	}



	
}
